package com.lensong.plugin.sentinel.service;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;



/**
 * Copyright （c）. All rights reserved.
 *
 * @author len.song
 * @date 2021/09/20
 */
@Service
public class SentinelSphUService {
    /**
     * 自定义资源名称
     */
    private static final String SOURCE_NAME1 = "lenEntryBySphU";

    public void entryBySphU(){

//        initFlowRules();

        // 1.5.0 版本开始可以直接利用 try-with-resources 特性
        try (Entry entry = SphU.entry(SOURCE_NAME1)) {
            // 被保护的逻辑
            System.out.println("hello world");
        } catch (BlockException ex) {
            // 处理被流控的逻辑
            System.out.println("blocked!");
        }

    }

    /*private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource(SOURCE_NAME1);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }*/
}
